package com.demo.common;

/**
 * @Author quan
 * @Description TODO
 * @Date 2023/8/9 12:24
 */

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * 这里用泛型
 * @param <T>
 */
@ApiModel("统一返回对象")
public class AppResult<T> {
    @JsonInclude(JsonInclude.Include.ALWAYS)
    @ApiModelProperty("状态码")
    private long code;

    @JsonInclude(JsonInclude.Include.ALWAYS)
    @ApiModelProperty("描述信息")
    private String message;

    @JsonInclude(JsonInclude.Include.ALWAYS)
    @ApiModelProperty("返回对象")
    private T data;//之前这里使用Object类型，现在我们用泛型

    //构造方法
    public AppResult(long code,String message){
        this.code = code;
        this.message = message;
    }
    public AppResult(long code,String message,T data){
        this.code = code;
        this.message = message;;
        this.data = data;
    }

    /**
     * get方法
     */
    public long getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public T getData() {
        return data;
    }

    //工厂方法
    /**
     * 成功
     */
    public static AppResult success(){
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage());
    }
    public static <T> AppResult<T> success (String message, T data){
        return new AppResult<>(ResultCode.SUCCESS.getCode(), message,data);
    }
    public static AppResult success(String message){
        return new AppResult(ResultCode.SUCCESS.getCode(),message);
    }
    public static <T> AppResult<T> success(T data){
        return new AppResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(),data);
    }
    /**
     * 失败方法
     */
    public static AppResult failed(){
        return new AppResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage());
    }
    public static AppResult failed(String message){
        return new AppResult(ResultCode.FAILED.getCode(),message);
    }
    public static AppResult failed(ResultCode resultCode){
        return new AppResult(resultCode.getCode(),resultCode.getMessage());
    }
}
